﻿#region Usings

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

#endregion

namespace Yandex.Market.Api.YmlSerialization
{
    /// <summary>
    ///   Holds list of exception occured during serialization/deserialization.
    /// </summary>
    internal class YmlParsingErrors
    {
        #region Fields

        /// <summary>
        ///   The list of exception occured during serialization/deserialization.
        /// </summary>
        private readonly List<KeyValuePair<YmlException, YmlExceptionTypes>> _listExceptions =
            new List<KeyValuePair<YmlException, YmlExceptionTypes>>();

        #endregion

        #region Properties

        /// <summary>
        ///   Gets a value indicating whether the list of errors is empty or not.
        /// </summary>
        /// <value><c>true</c> if the list is not empty; otherwise, <c>false</c>.</value>
        public bool ContainsAnyError
        {
            get { return _listExceptions.Count > 0; }
        }

        /// <summary>
        ///   Gets the number of errors within the list of errors.
        /// </summary>
        /// <value>the number of errors within the list of errors.</value>
        public int Count
        {
            get { return _listExceptions.Count; }
        }

        #endregion

        #region Indexers

        /// <summary>
        ///   Gets the the pair of Exception and its corresponding exception-type with the specified n.
        /// </summary>
        /// <param name = "n">The index of the exception/exception type pair in the error list to return.</param>
        /// <value></value>
        public KeyValuePair<YmlException, YmlExceptionTypes> this[int n]
        {
            get { return _listExceptions[n]; }
        }

        #endregion

        #region Methods

        /// <summary>
        ///   Adds an exception with the corresponding type.
        /// </summary>
        /// <param name = "exception">The exception to add.</param>
        /// <param name = "exceptionType">Type of the exception added.</param>
        public void AddException(YmlException exception, YmlExceptionTypes exceptionType)
        {
            _listExceptions.Add(new KeyValuePair<YmlException, YmlExceptionTypes>(exception, exceptionType));
        }

        /// <summary>
        ///   Clears the list of errors.
        /// </summary>
        public void ClearErrors()
        {
            _listExceptions.Clear();
        }

        /// <summary>
        ///   Adds the list of errors within another instance of <c>YmlParsingErrors</c>.
        /// </summary>
        /// <param name = "parsingErrors">The parsing errors to add its content.</param>
        public void AddRange(YmlParsingErrors parsingErrors)
        {
            parsingErrors._listExceptions.ForEach(_listExceptions.Add);
        }

        /// <summary>
        ///   Returns a <see cref = "T:System.String" /> that represents the current <see cref = "T:System.Object" />.
        /// </summary>
        /// <returns>
        ///   A <see cref = "T:System.String" /> that represents the current <see cref = "T:System.Object" />.
        /// </returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            _listExceptions.ForEach(pair =>
                                    {
                                        sb.AppendLine(String.Format(CultureInfo.CurrentCulture,
                                                                    "{0,-8} : {1}",
                                                                    pair.Value,
                                                                    pair.Key.Message));
                                        sb.AppendLine();
                                    });

            return sb.ToString();
        }

        #endregion
    }
}